﻿using Business.Base.Common;
using Business.Base.Enums;
using Business.Base.Model;
using Business.ScrewPump.Perf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Pumptester.ExperimentForms.ScrewPump.Perf
{
    public partial class ScrewPumpEditForm : Pumptester.ExperimentForms.Base.BaseEditForm
    {
        public ScrewPumpEditForm()
        {
            InitializeComponent();
        }

        public ScrewPumpEditForm(Experiment experiment):base(experiment)
        {
            InitializeComponent();
        }

        void BandCombox()
        {
            Dictionary<ComboBox, DataSource[]> boxDic = new Dictionary<ComboBox, DataSource[]>();

            boxDic.Add(comboBoxFlowmeter, DataSource.DataSourceDic[DataSource.FLOWMETER]);

            boxDic.Add(comboBoxWindingConnection, DataSource.DataSourceDic[DataSource.WINDINGCONNECTION]);
            boxDic.Add(comboBoxWindingMaterial, DataSource.DataSourceDic[DataSource.WINGDINGMATERIAL]);
            boxDic.Add(comboBoxLevel, DataSource.DataSourceDic[DataSource.PRESUREFLOWLEVEL]);
            boxDic.Add(comboBoxRateVoltage, DataSource.DataSourceDic[DataSource.MOTORRATEDVOLTAGE]);


            foreach (KeyValuePair<ComboBox, DataSource[]> pair in boxDic)
            {
                pair.Key.DataSource = pair.Value;
                pair.Key.DisplayMember = "BindMember";
                pair.Key.ValueMember = "BindValue";
                pair.Key.SelectedIndex = 0;
            }
            //this.comboBoxPumpType.SelectedValue = pumpType;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
          
            ScrewPumpPerfExperiment experiment = (ScrewPumpPerfExperiment)Experiment;
            experiment.ExperimentCode = this.textBoxCode.Text;
            experiment.PumpName = this.textBoxPumpName.Text;
            experiment.PumpModel = this.textBoxProductModel.Text;
            experiment.PumpCode = this.textBoxPumpCode.Text;
            experiment.ExperimentOwner = this.textBoxExperimentOwner.Text;
            experiment.Manufacturer = this.textBoxManufacturer.Text;
            experiment.Customer = this.textBoxCustomer.Text;

            experiment.PumpRatedFlow = Convert.ToDouble(this.textBoxRatedFlow.Text);
            experiment.PumpRatedPresure = Convert.ToDouble(this.textBoxRatedPresure.Text);
            experiment.PumpRatedPower = Convert.ToDouble(this.textBoxRatedPower.Text);
            experiment.PumpRatedEfficiency = Convert.ToDouble(this.textBoxRatedEff.Text);
            experiment.MotorRatedSpeed = Convert.ToDouble(this.textBoxRatedSpeed.Text);
       
            experiment.ImpellerDia = Convert.ToDouble(this.textBoxImpellerDia.Text);
            experiment.MotorRatedVoltage = (MotorRatedVoltage)Convert.ToInt32(this.comboBoxRateVoltage.SelectedValue);
            experiment.MotorRatedCurrent = Convert.ToDouble(this.textBoxRatedCurrent.Text);
            experiment.MotorRatedEfficiency = Convert.ToDouble(this.textBoxMotorEff.Text);
            experiment.MotorWindingConnection = (string)this.comboBoxWindingConnection.SelectedValue;
            experiment.MotorWindingMaterial = (String)this.comboBoxWindingMaterial.SelectedValue;

            experiment.MotorRatedEfficiency = Convert.ToDouble(this.textBoxMotorEff.Text);

            experiment.DInlet = Convert.ToDouble(this.textBoxDInlet.Text) / 1000;
            experiment.DOutlet = Convert.ToDouble(this.textBoxDOutlet.Text) / 1000;

            experiment.ZInlet = Convert.ToDouble(this.textBoxZInLet.Text) / 1000;
            experiment.ZOutlet = Convert.ToDouble(this.textBoxZOutLet.Text) / 1000;

            experiment.Flowmeter = comboBoxFlowmeter.Text;
            experiment.FlowmeterAddress = (int)comboBoxFlowmeter.SelectedValue;
            //experiment.ExperimentType = ExperimentType.Perf;
            experiment.ExperimentLevel = (PresureFlowLevel)this.comboBoxLevel.SelectedValue;
            experiment.CreateTime = DateTime.Now;
            experiment.ExperimentType = ExperimentType.ScrewPumpPerf;
            //experiment.TransformationRatio = double.Parse(this.comboBoxCurrentTransRatio.SelectedValue.ToString());
            experiment.TransformationRatio = Convert.ToDouble(this.textBoxCurrentTransRatio.Text);
            //experiment.TransFormRadio = Convert.ToDouble(this.textBoxCurrentTransRatio.se);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ScrewPumpEditForm_Load(object sender, EventArgs e)
        {
            BandCombox();
            ScrewPumpPerfExperiment screwPumpPerfExperiment = (ScrewPumpPerfExperiment)this.Experiment;

            this.textBoxPumpName.Text = screwPumpPerfExperiment.PumpName;

            this.textBoxProductModel.Text = screwPumpPerfExperiment.PumpModel;
            this.textBoxPumpCode.Text = screwPumpPerfExperiment.PumpCode;
            this.textBoxExperimentOwner.Text = screwPumpPerfExperiment.ExperimentOwner;
            this.textBoxManufacturer.Text = screwPumpPerfExperiment.Manufacturer;


            this.textBoxRatedFlow.Text = screwPumpPerfExperiment.PumpRatedFlow.ToString();
            this.textBoxRatedPresure.Text = screwPumpPerfExperiment.PumpRatedPresure.ToString();
            this.textBoxRatedPower.Text = screwPumpPerfExperiment.PumpRatedPower.ToString();
            this.textBoxRatedEff.Text = screwPumpPerfExperiment.PumpRatedEfficiency.ToString();

            this.textBoxRatedSpeed.Text = screwPumpPerfExperiment.MotorRatedSpeed.ToString();

            this.textBoxImpellerDia.Text = screwPumpPerfExperiment.ImpellerDia.ToString();

            this.comboBoxRateVoltage.SelectedValue = (int)screwPumpPerfExperiment.MotorRatedVoltage;
            this.textBoxRatedCurrent.Text = screwPumpPerfExperiment.MotorRatedCurrent.ToString();

            this.comboBoxWindingConnection.SelectedValue = screwPumpPerfExperiment.MotorWindingConnection;
            this.comboBoxWindingMaterial.SelectedValue = screwPumpPerfExperiment.MotorWindingMaterial;
            this.textBoxMotorEff.Text = screwPumpPerfExperiment.MotorRatedEfficiency.ToString();

            this.textBoxDInlet.Text = (screwPumpPerfExperiment.DInlet * 1000).ToString();
            this.textBoxDOutlet.Text = (screwPumpPerfExperiment.DOutlet * 1000).ToString();

            this.textBoxZInLet.Text = (screwPumpPerfExperiment.ZInlet * 1000).ToString();
            this.textBoxZOutLet.Text = (screwPumpPerfExperiment.ZOutlet * 1000).ToString();

            comboBoxFlowmeter.Text = screwPumpPerfExperiment.Flowmeter;
            comboBoxFlowmeter.SelectedValue = screwPumpPerfExperiment.FlowmeterAddress;
            this.comboBoxLevel.SelectedValue = screwPumpPerfExperiment.ExperimentLevel;

            //this.comboBoxFlowmeter.SelectedValue = screwPumpPerfExperiment.FlowmeterAddress;
            //this.comboBoxCurrentTransRatio.SelectedValue = screwPumpPerfExperiment.TransformationRatio.ToString();
            this.textBoxCurrentTransRatio.Text = screwPumpPerfExperiment.TransformationRatio.ToString();

            //this.textBoxCurrentTransRatio.Text = screwPumpPerfExperiment.TransFormRadio.ToString();

            this.textBoxCustomer.Text = screwPumpPerfExperiment.Customer;
        }
    }
}
